package com.example.databasetest;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MyDbHelper myHelper = new MyDbHelper(this,"BookStore.db", null, 1);
        //得到操作数据库的对象db
        this.db = myHelper.getWritableDatabase();


        Button addData = (Button) findViewById(R.id.add_data);
        addData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity.this.db.execSQL("insert into book values(null, '张三',60,100,'如何学习')");

                ContentValues values = new ContentValues();
                // 开始组装第一条数据
                values.put("name", "The Da Vinci Code");
                values.put("author", "Dan Brown");
                values.put("pages", 454);
                values.put("price", 16.96);
                db.insert("Book", null, values); // 插入第一条数据
                values.clear();
                // 开始组装第二条数据
                values.put("name", "The Lost Symbol");
                values.put("author", "Dan Brown");
                values.put("pages", 510);
                values.put("price", 19.95);
                db.insert("Book", null, values); // 插入第二条数据
//nullColumnHack： 当values参数为空或者里面没有内容的时候，我们insert是会失败的
// （底层数据库不允许插入一个空行），为了防止这种情况，我们要在这里指定一个 列名，
// 到时候如果发现将要插入的行为空行时，就会将你指定的这个列名的值设为null，然后再向数据库中插入。
                Toast.makeText(MainActivity.this,"添加数据成功",Toast.LENGTH_SHORT).show();
            }
        });
        Button updateData = (Button) findViewById(R.id.update_data);
        updateData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ContentValues values = new ContentValues();
                values.put("price", 10.99);
                db.update("Book", values, "name = ?", new String[] { "The Da Vinci Code" });

                Toast.makeText(MainActivity.this,"修改数据成功",Toast.LENGTH_SHORT).show();
            }
        });
        Button deleteButton = (Button) findViewById(R.id.delete_data);
        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //db.delete("Book", "pages > ?", new String[] { "500" });
                db.execSQL("delete from book where pages > 500");
                Toast.makeText(MainActivity.this,"删除数据成功",Toast.LENGTH_SHORT).show();
            }
        });
        Button queryButton = (Button) findViewById(R.id.query_data);
        queryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 查询Book表中所有的数据
                //Cursor cursor = db.query("Book", null, null, null, null, null, null);

                Cursor cursor = db.rawQuery("select * from book",null);
                if (cursor.moveToFirst()) {
                    do {
                        // 遍历Cursor对象，取出数据并打印

                         //取出列索引去得到相应的值,@SuppressLint("Range")忽略指定的警告
                        @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex("name"));
                        @SuppressLint("Range") String author = cursor.getString(cursor.getColumnIndex("author"));
                        @SuppressLint("Range") int pages = cursor.getInt(cursor.getColumnIndex("pages"));
                        @SuppressLint("Range") double price = cursor.getDouble(cursor.getColumnIndex("price"));
                        Log.d("my", "book name is " + name);
                        Log.d("my", "book author is " + author);
                        Log.d("my", "book pages is " + pages);
                        Log.d("my", "book price is " + price);
                    } while (cursor.moveToNext());
                }
                cursor.close();
            }
        });
    }

//页面关掉的时候关掉数据库
    @Override
    protected void onDestroy() {
        db.close();
        super.onDestroy();
    }
}
